import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:get/get.dart' show Get;
import 'package:jinke/global/user/logic.dart';
import 'package:jinke/global/user/user_info.dart';
import 'package:jinke/page/pro_list/subs/pro_detail/model/pro_com_model.dart';
import 'package:jinke/page/pro_list/subs/pro_detail/model/pro_open_factor_model.dart';
import 'package:jinke/utils/httputils/resultdata.dart';

import 'httpmanager.dart';
import 'urlpath.dart';

/// 所有接口类
class Api {
  static post({
    required String url,
    Map<String, dynamic>? param,
    bool withLoading = true,
  }) {
    return HttpManager.getInstance().post(
      url,
      params: param,
      withLoading: withLoading,
    );
  }

  static get({
    required String url,
    Map<String, dynamic>? param,
    bool withLoading = true,
  }) {
    return HttpManager.getInstance().get(
      url,
      params: param,
      withLoading: withLoading,
    );
  }

  //登录
  static login(Map<String, dynamic> param) {
    return HttpManager.getInstance().post(UrlPath.login, params: param);
  }

  //领导批示
  static listPageDirectives(Map<String, dynamic> param) {
    return HttpManager.getInstance()
        .post(UrlPath.listPageDirectives, params: param);
  }

  //做批示
  static saveDirective(Map<String, dynamic> param) {
    return HttpManager.getInstance().post(UrlPath.saveDirective, params: param);
  }

  //每日报告列表
  static listPageReports(Map<String, dynamic> param) {
    return HttpManager.getInstance()
        .post(UrlPath.listPageReports, params: param);
  }

  //每日报告列表
  static generateGatherReport(Map<String, dynamic> param) {
    return HttpManager.getInstance()
        .post(UrlPath.generateGatherReport, params: param);
  }

  //获取下载地址
  static saProjectReportShare(String reportDate) {
    return HttpManager.getInstance().get(UrlPath.saProjectReportShare, params: {
      'reportDate': reportDate,
      'tenantId': 'ZZ-JSQ',
      'userId': UserController.instance.state.user!.userId ?? ''
    });
  }

  //每日报告详情
  static getReportDetail(Map<String, dynamic> param) {
    return HttpManager.getInstance()
        .post(UrlPath.getReportDetail, params: param);
  }

  //编辑报告
  static batchSave(dynamic param) {
    return HttpManager.getInstance().post(UrlPath.batchSave, params: param);
  }

  //每日报告已读
  static saveDyaReportRead(Map<String, dynamic> param) {
    return HttpManager.getInstance()
        .post(UrlPath.saveDyaReportRead, params: param);
  }

  //每日报告详情数据
  static getReportStatistics(Map<String, dynamic> param) {
    return HttpManager.getInstance()
        .post(UrlPath.getReportStatistics, params: param);
  }

  //统计
  static getProjectStatisticalData(Map<String, dynamic> param) {
    return HttpManager.getInstance()
        .post(UrlPath.getProjectStatisticalData, params: param);
  }

  //统计-区域
  static listCentralStatisticalData(Map<String, dynamic> param) {
    return HttpManager.getInstance()
        .post(UrlPath.listCentralStatisticalData, params: param);
  }

  //招引工作列表
  static listWorks(Map<String, dynamic> param) {
    return HttpManager.getInstance().post(UrlPath.listWorks, params: param);
  }

  //保存招引工作
  static saveWorks(List param) {
    return HttpManager.getInstance().post(UrlPath.saveWorks, params: param);
  }

  // 多文件上传
  static postUploadFiles(List<MultipartFile> multipartFiles,
      {CancelToken? cancelToken}) {
    return HttpManager.getInstance().uploadFiles(UrlPath.postUploadFiles,
        multipartFiles: multipartFiles, cancelToken: cancelToken);
  }
}

class ProListApi extends Api {
  static Future postProList({
    bool isMine = false,
    String? toParticipate,
    String? order,
    String? money,
  }) {
    return Api.post(
      url: UrlPath.prolist,
      param: {
        'createBy': isMine ? (UserController().state.user?.userId ?? '') : '',
        'toParticipate': toParticipate ?? '',
        'orderColumn': order ?? '',
        'moneyScreening': money ?? '',
      },
    );
  }

  static Future getFilters({
    required String codeType,
  }) {
    return Api.get(
      url: UrlPath.pro_filter_types,
      param: {
        'codeType': codeType,
        'tenantId': UserController().state.user?.tenantId ?? '',
      },
    );
  }

  static Future focus({
    required String id,
  }) {
    return Api.get(
      url: UrlPath.pro_focus,
      param: {
        'id': id,
      },
    );
  }

  static Future detail({
    required String id,
  }) {
    return Api.get(
      url: UrlPath.pro_detail,
      param: {
        'id': id,
      },
    );
  }

  static Future postProDayList({
    required String id,
  }) {
    return Api.post(url: UrlPath.pro_progress_day, param: {
      'entity': {
        'projectIds': [id],
      },
      'query': {
        'pageNum': 1,
        'pageSize': 1000,
        'sortField': "",
        'sortOrder': ""
      }
    });
  }

  static Future postInventoryTab({
    required String id,
  }) {
    return Api.post(url: UrlPath.pro_inventory_tab, param: {
      'dataType': 'LISTING_1',
      'userId': UserController().state.user?.userId ?? '',
      'id': id,
    });
  }

  static Future postInventoryList({
    required String busId,
    required String personType,
    String dateType = 'LISTING_1',
  }) {
    return Api.post(url: UrlPath.pro_inventory_list, param: {
      'dataType': dateType,
      'busType': "PROJECT_ID",
      'personType': personType,
      'busId': busId,
      'pageNum': 1,
      'pageSize': 100,
    });
  }

  static Future getComList() {
    return Api.get(
      url: UrlPath.com_list,
    );
  }

  static Future createStep1({
    required String codeType,
  }) {
    return Api.get(
      url: UrlPath.pro_factors,
      param: {
        'codeType': codeType,
        'tenantId': UserController().state.user?.tenantId ?? '',
      },
    );
  }

  static Future open({
    required ProComModel com,
    required List<ProOpenFactorModel> factors,
    required Map<String, dynamic> parm,
  }) {
    return Api.post(url: UrlPath.pro_open, param: {
      'saveOrEditEnterprise': com.toJson(),
      'userId': UserController().state.user?.userId ?? '',
      'saveOrEditPhases': factors.map((e) => e.toParm()).toList(),
      'saveOrEditProject': parm
    });
  }

  static Future createQues({
    required String name,
    required String reslve,
    required String time,
    required String status,
    required UserInfo lead,
    required List<UserInfo> helps,
  }) {
    return Api.post(url: UrlPath.create_ques, param: {
      'busId': "488b8225-4775-4183-834f-7f0d44106bbb",
      'busType': "PROJECT_ID",
      'completeTime': time,
      'dataStatus': "LISTING_STATUS_$status",
      'dataType': "LISTING_2",
      'enforcementMeasures': reslve,
      'name': name,
      'objInfosLead': [
        {'name': lead.userName ?? '', 'id': lead.userId}
      ],
      'objInfosSynergy':
          helps.map((e) => {'name': e.userName ?? '', 'id': e.userId}).toList(),
    });
  }

  static Future createTask({
    required String name,
    required String reslve,
    required String time,
    required String status,
    required UserInfo lead,
    required List<UserInfo> helps,
  }) {
    return Api.post(url: UrlPath.create_ques, param: {
      'busId': "488b8225-4775-4183-834f-7f0d44106bbb",
      'busType': "PROJECT_ID",
      'completeTime': time,
      'dataStatus': "LISTING_STATUS_$status",
      'dataType': "LISTING_1",
      'enforcementMeasures': reslve,
      'name': name,
      'objInfosLead': [
        {'name': lead.userName ?? '', 'id': lead.userId}
      ],
      'objInfosSynergy':
          helps.map((e) => {'name': e.userName ?? '', 'id': e.userId}).toList(),
    });
  }
}

class MeApi extends Api {
  static Future getUserList() {
    return Api.get(
      url: UrlPath.userList,
    );
  }
}
